package com.kingyea.mobilepolice.config.feign;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author cxc
 * @version v1.0.0
 * @description This is a class
 * @date Created in 2018.04.18 09:04
 */
@Component
@Configuration
public class RestTemplateConfig {

    private static final String AUTHORIZATION_HEADER = "Authorization";

    private static final String BEARER_TOKEN_TYPE = "Bearer";

    @Bean
    @Qualifier("restTemplateNei")
    public RestTemplate restTemplateNei(@Qualifier("practiceOAuth2RestTemplateNei") OAuth2RestTemplate oAuth2RestTemplate) {
        RestTemplate restTemplate = new RestTemplate();
        setHeader(restTemplate, oAuth2RestTemplate);
        return restTemplate;
    }

    @Bean
    @Qualifier("restTemplate")
    @Primary
    public RestTemplate restTemplate(@Qualifier("practiceOAuth2RestTemplate") OAuth2RestTemplate oAuth2RestTemplate) {
        RestTemplate restTemplate = new RestTemplate();
        setHeader(restTemplate, oAuth2RestTemplate);
        return restTemplate;
    }

    private void setHeader(RestTemplate restTemplate, OAuth2RestTemplate oAuth2RestTemplate){
        restTemplate.setInterceptors(Lists.newArrayList((request, body, execution) -> {
            request.getHeaders().add(AUTHORIZATION_HEADER,
                    String.format("%s %s",
                            BEARER_TOKEN_TYPE,
                            oAuth2RestTemplate.getAccessToken().toString()));
            return execution.execute(request, body);
        }));
    }


    @LoadBalanced
    @Bean
    @Qualifier("initRestTemplate")
    public RestTemplate initRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> coverters = restTemplate.getMessageConverters();
        coverters.add(fastJsonHttpMessageConverter());
        return restTemplate;
    }

    /**
     * // 该转换器会改变feign对 DefaultOAuth2AccessToken 对象的封装
     * 不启用该转换器：
     * {
     *     "access_token": "dd8a09cc-6404-475e-82f6-d541642cd58f",
     *     "token_type": "bearer",
     *     "refresh_token": "f8e9ecd3-2d2f-493c-aa31-8e7143f2cbac",
     *     "expires_in": 167776,
     *     "scope": "xx"
     * }
     * 启用该转换器：
     * {
     *     "additionalInformation": {},
     *     "expiration": 1534335006664,
     *     "expired": false,
     *     "expiresIn": 167552,
     *     "refreshToken": {
     *         "expiration": 1535235006651,
     *         "value": "f8e9ecd3-2d2f-493c-aa31-8e7143f2cbac"
     *     },
     *     "scope": [
     *         "xx"
     *     ],
     *     "tokenType": "bearer",
     *     "value": "dd8a09cc-6404-475e-82f6-d541642cd58f"
     * }
     * */
//    @Bea
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        return new FastJsonHttpMessageConverter();
    }
//    @Bean
//    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
//        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.FALSE);
//        jsonConverter.setObjectMapper(objectMapper);
//        return jsonConverter;
//    }

}
